<?php
/**
*
* @package ppkBB3cker
* @version $Id: topdown_torrents_add1.php 1.000 2010-10-30 18:39:00 PPK $
* @copyright (c) 2010 PPK
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/


define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('viewforum');

$dt=time();
$topdown_torrents_fid=request_var('fid', 0);
$topdown_torrents_pid=request_var('pid', 0);
$topdown_torrents='';
$ppkbb_topdown_torrents=my_split_config($config['ppkbb_topdown_torrents'], 14, array('my_int_val', 'my_int_val', 'my_int_val', 'my_int_val', 'strval', 'my_int_val', 'my_int_val', 'my_int_val', 'my_int_val', 'my_int_val', 'my_int_val', 'my_int_val', 'my_int_val', 'my_int_val'));

if(!$auth->acl_get('u_canviewtopdowntorrents') || (!$ppkbb_topdown_torrents[0] && $topdown_torrents_pid && !$topdown_torrents_fid) || (!$ppkbb_topdown_torrents[1] && !$topdown_torrents_fid && !$topdown_torrents_pid) || (!$ppkbb_topdown_torrents[2] && $topdown_torrents_fid && !$topdown_torrents_pid))
{
	$template->assign_vars(array(
		'TOPDOWN_TORRENTS_POSTERS' => false,
	));
}
else
{
	$ppkbb_topdown_torrents_exclude=$r_torrents=$torrents_posters=array();
	if($config['ppkbb_topdown_torrents_exclude'])
	{
		$ppkbb_topdown_torrents_exclude=my_split_config($config['ppkbb_topdown_torrents_exclude'], 0, 'my_int_val', ',');
	}
	$disallow_access=array_unique(array_keys($auth->acl_getf('!f_read', true)));
	if($config['ppkbb_topdown_torrents_trueexclude'])
	{
		$ppkbb_topdown_torrents_exclude=array_unique(array_merge($ppkbb_topdown_torrents_exclude, $disallow_access));
		$disallow_access=array();
	}
	$ppkbb_tcenable_rannounces=my_split_config($config['ppkbb_tcenable_rannounces'], 9, 'my_int_val');

	$sql="SELECT
		a.attach_id, a.post_msg_id, a.extension, a.real_filename, a.physical_filename, a.i_width, a.i_height, a.i_external, tt.topic_title, tt.topic_id, tt.forum_id, tt.topic_first_post_id, t.times_completed".($ppkbb_tcenable_rannounces[0] ? '+t.rem_times_completed' : '')." times_completed, t.leechers".($ppkbb_tcenable_rannounces[0] ? '+t.rem_leechers' : '')." leechers, t.seeders".($ppkbb_tcenable_rannounces[0] ? '+t.rem_seeders' : '')." seeders, t.id torrent_id"
		.($ppkbb_topdown_torrents[12]==1 || $ppkbb_topdown_torrents[13]==1 ? ', p.post_text, p.enable_smilies, p.enable_bbcode, p.enable_magic_url, p.bbcode_bitfield, p.bbcode_uid' : '').
		" FROM ".ATTACHMENTS_TABLE." a, ".TOPICS_TABLE." tt, ". TRACKER_TORRENTS_TABLE ." t"
		.($ppkbb_topdown_torrents[12]==1 || $ppkbb_topdown_torrents[13]==1 ? ', '.POSTS_TABLE.' p' : '').
		" WHERE ".(sizeof($ppkbb_topdown_torrents_exclude) ? " t.forum_id ".($config['ppkbb_topdown_torrents_trueexclude'] ? 'NOT '  : '')."IN('".implode("', '", $ppkbb_topdown_torrents_exclude)."') AND " : '').(sizeof($disallow_access) ? " t.forum_id NOT IN('".implode("', '", $disallow_access)."') AND " : '').($topdown_torrents_fid ? " t.forum_id='{$topdown_torrents_fid}' AND " : '')."a.post_msg_id=tt.topic_first_post_id AND tt.topic_id=t.topic_id ".($ppkbb_topdown_torrents[7] ? "AND t.seeders".($ppkbb_tcenable_rannounces[0] ? '+t.rem_seeders' : '')." >= {$ppkbb_topdown_torrents[7]} " : '')
		." AND (a.i_poster='1'".($ppkbb_topdown_torrents[9] ? " AND a.thumbnail='1'" : '').")"
		.($ppkbb_topdown_torrents[10] ? " AND t.added > ".($dt-($ppkbb_topdown_torrents[10]*86400)) : '').($ppkbb_topdown_torrents[12]==1 || $ppkbb_topdown_torrents[13]==1 ? ' AND p.post_id=tt.topic_first_post_id' : '')." ORDER BY ".($ppkbb_topdown_torrents[11] ? 't.id' : 't.times_completed'.($ppkbb_tcenable_rannounces[0] ? '+t.rem_times_completed' : ''))." DESC LIMIT 0, {$ppkbb_topdown_torrents[3]}";

	$result=$db->sql_query($sql, $ppkbb_topdown_torrents[6]*60);
	while($row=$db->sql_fetchrow($result))
	{
		$torrents_posters[$row['torrent_id']]=$row;
		$r_torrents[$row['torrent_id']]=$row['torrent_id'];
	}
	$db->sql_freeresult($result);

	if($ppkbb_topdown_torrents[8] && sizeof($torrents_posters) < $ppkbb_topdown_torrents[8])
	{
		$template->assign_vars(array(
			'TOPDOWN_TORRENTS_POSTERS' => false,
			)
		);
	}
	else
	{
		$tooltip_tdt='';
		$i=0;
		if($ppkbb_topdown_torrents[12]==1 || $ppkbb_topdown_torrents[13]==1)
		{
			$text_length=2000;
			include_once($phpbb_root_path . 'includes/bbcode.' . $phpEx);
			$bbcode = new bbcode();
		}
		foreach($torrents_posters as $k => $v)
		{
			if($ppkbb_topdown_torrents[12]==1 || $ppkbb_topdown_torrents[13]==1)
			{
				$message = $v['post_text'];

				// Parse the message and subject
				$message = censor_text($message);

				// Second parse bbcode here
				if ($v['bbcode_bitfield'])
				{
					$bbcode->bbcode_second_pass($message, $v['bbcode_uid'], $v['bbcode_bitfield']);
				}
				$message = bbcode_nl2br($message);
				$message = smiley_text($message);

				if ($text_length != 0 && (strlen($message) > $text_length))
				{
					$message = closetags(substr($message, 0, $text_length));
					$message .= '...';
				}

				$v['post_text']= $message;
			}
			$v['i_height'] ? $i_factor=my_float_val($v['i_width']/$v['i_height']) : $i_factor=0.675;
			$i_width=my_int_val($ppkbb_topdown_torrents[5]*$i_factor);
			$t_title=$v['topic_title']. " - {$user->lang['TORRENT_COMPLETED']}: ".($v['times_completed'])." {$user->lang['TORRENT_SEEDERS']}: ".($v['seeders'])." {$user->lang['TORRENT_LEECHERS']}: ".($v['leechers']);
			$tdt_image='<img class="tdt_image" rel="tdt'.$i.'"'.($ppkbb_topdown_torrents[12]==1 || $ppkbb_topdown_torrents[13]==1 ? ' align="left" style="float:left;margin:5px;"' : '').' src="'.($v['i_external'] ? $v['real_filename'] : append_sid("{$phpbb_root_path}download/file.$phpEx", 'id=' . $v['attach_id']).($ppkbb_topdown_torrents[9] ? '&amp;t=1' : '')).'" alt="'.$t_title.'" height="'.($ppkbb_topdown_torrents[13]==2 ? '%d' : $ppkbb_topdown_torrents[5]).'" width="'.($ppkbb_topdown_torrents[13]==2 ? '%d' : $i_width).'" />';
			$topdown_torrents.='<div class="panel"><a href="'.append_sid($phpbb_root_path.'viewtopic.' . $phpEx . '?t=' . $v['topic_id'] . '&amp;f=' . $v['forum_id']).'"'.($ppkbb_topdown_torrents[12]==1 || ($ppkbb_topdown_torrents[12]!=1 && !$ppkbb_topdown_torrents[13]) ? ' title = "'.$t_title.'"' : '').'>'.($ppkbb_topdown_torrents[13]==2 ? sprintf($tdt_image, $ppkbb_topdown_torrents[5], $i_width) : $tdt_image).'</a>'.($ppkbb_topdown_torrents[12]==1 ? $v['post_text'] : '').'</div>';
			if($ppkbb_topdown_torrents[13] && $ppkbb_topdown_torrents[12]!=3)
			{
				$tooltip_tdt.='<div style="display:none;" id="tdt'.$i.'">'.($ppkbb_topdown_torrents[13]==2 ? sprintf($tdt_image, $ppkbb_topdown_torrents[5]*2, $i_width*2) : $tdt_image).($ppkbb_topdown_torrents[13]==1 ? $v['post_text'] : '').'</div>';
			}
			$i+=1;
		}
		$tdt_count=sizeof($torrents_posters);
		$template->assign_vars(array(
			'TOPDOWN_TORRENTS_POSTERS' => $topdown_torrents ? $topdown_torrents : false,
			'TOPDOWN_TORRENTS_COUNT' => $tdt_count,
			'S_TOPDOWN_TORRENTS' => ($topdown_torrents_fid && @$forum_data['forum_name'] ? ($ppkbb_topdown_torrents[11] ? sprintf($user->lang['TOPDOWN_TORRENTS_ASNEWTORRENTS_INFORUM'], $tdt_count, $forum_data['forum_name']) : sprintf($user->lang['TOPDOWN_TORRENTS_INFORUM'], $tdt_count, $forum_data['forum_name'])) : ($ppkbb_topdown_torrents[11] ? sprintf($user->lang['TOPDOWN_TORRENTS_ASNEWTORRENTS'], $tdt_count) : sprintf($user->lang['TOPDOWN_TORRENTS'], $tdt_count))),
			'S_TOPDOWN_TORRENTS_WIDTH' => $ppkbb_topdown_torrents[4],
			'S_TOPDOWN_TORRENTS_WIDTH2' => $ppkbb_topdown_torrents[12]==1 ? $ppkbb_topdown_torrents[5]*2 : false,
			'S_TOPDOWN_TORRENTS_HEIGHT' => $ppkbb_topdown_torrents[5]+10,
			'S_TOPDOWN_TORRENTS_BUTTPOS' => my_int_val($ppkbb_topdown_torrents[5]/2),
			'S_SEL_TDT' => $ppkbb_topdown_torrents[12] ? true : false,
			'S_TDT_TYPE' => $ppkbb_topdown_torrents[12],
			'S_TDT_TOOLTIPS' => $tooltip_tdt ? $tooltip_tdt : false,
			)
		);
	}
}
// Output page
page_header($user->lang['INDEX']);

$template->set_filenames(array(
	'body' => 'topdown_torrents_body.html')
);

page_footer();

//###############################################################################
function closetags($text, $ex=array('area', 'base', 'basefont', 'br', 'col', 'frame', 'hr', 'img', 'input', 'isindex', 'link', 'meta', 'param'))
{
	$text = substr($text, 0, strrpos($text," "));
	$text = preg_replace("/<[^>]*$/i", "", $text);
	preg_match_all("/<[^a-z>\/]*([a-z]{1,50})/i", $text, $otags);
	if(count($otags[0])>0)
	{
		$fotags=$fctags=array();
		preg_match_all("/<[ t]*\/[^a-z]*([a-z]{1,50})/i", $text, $ctags);
		foreach($otags[1] as $otag)
		{
			$otag = strtolower($otag);
			if(isset($fotags[$otag]))
			{
				$fotags[$otag]++;
			}
			else
			{
				$fotags[$otag] = 1;
			}
		}
		foreach($ctags[1] as $ctag)
		{
			$ctag = strtolower($ctag);
			if(isset($fctags[$ctag]))
			{
				$fctags[$ctag]++;
			}
			else
			{
				$fctags[$ctag] = 1;
			}
		}
		while(list($tag, $cnt) = each($fotags))
		{
			if(in_array($tag, $ex))
			{
				continue;
			}
			$fctags[$tag] = isset($fctags[$tag]) ? $fctags[$tag] : 0;
			$text.=str_repeat("</$tag>", abs($fctags[$tag] - $cnt));
		}
	}
	return $text;
}
?>
